﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;

namespace BOH.DataManagement
{
    public partial class TestModuleManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TestTypeBind();
            }
        }

        public void TestTypeBind()
        {
            DataSet dst = SysConfigClass.getAllATestModule();

            BindTestTypeList(dst);
        }

        public void BindTestTypeList(DataSet dst)
        {
            gvTestModuleList.DataSource = dst;
            gvTestModuleList.DataBind();
        }

        public string GetModule(string moduleID)
        {
            string returnValue = string.Empty;
            if (moduleID.Trim() == "2")
                returnValue = "去除模式";
            else
                returnValue = "循环模式";

            return returnValue;
        }

        protected void gvTestModuleList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvTestModuleList.EditIndex = e.NewEditIndex;
            TestTypeBind();
        }

        protected void gvTestModuleList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)))
            {
                DropDownList drpTestModule = (DropDownList)e.Row.FindControl("drpTestModule");
                Button butRefresh = (Button)e.Row.FindControl("butRefresh");
                drpTestModule.SelectedValue = ((HiddenField)e.Row.FindControl("hideTestModule")).Value.Trim();
                if (drpTestModule.SelectedValue == "2")
                {
                    butRefresh.Visible = true;
                }
                else
                    butRefresh.Visible = false;
            }
        }

        protected void gvTestModuleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvTestModuleList.EditIndex = -1;
            TestTypeBind();
        }

        protected void gvTestModuleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow gvr = gvTestModuleList.Rows[e.RowIndex];
            string TestModuleID = ((HiddenField)gvr.FindControl("hideSN")).Value;
            string TestModule = ((DropDownList)gvr.FindControl("drpTestModule")).SelectedValue;
            SysConfigClass.UpdateTestModule(TestModuleID, TestModule);

            gvTestModuleList.EditIndex = -1;
            TestTypeBind();
        }

        protected void TestModuleChanged(object sender, System.EventArgs e)
        {
            GridViewRow item = (GridViewRow)((Control)sender).Parent.Parent;
            DropDownList list;
            list = (DropDownList)item.FindControl("drpTestModule");
            Button butRefresh;
            butRefresh = (Button)item.FindControl("butRefresh");
            if (list.SelectedValue == "2")
            {
                butRefresh.Visible = true;
            }
            else
                butRefresh.Visible = false;
        }

        protected void RefreshCanUse(object sender, System.EventArgs e)
        {
            GridViewRow item = (GridViewRow)((Control)sender).Parent.Parent;
            HiddenField hid;
            hid = (HiddenField)item.FindControl("hideSubjectID");
            string period = (string)Session["ApplyPeriod"];
            InterviewClass.DisableCanUseBySubject(hid.Value, period);
            gvTestModuleList.EditIndex = -1;
            TestTypeBind();
        }

    }
}
